package com.jzoom.zoom.admin.entities;

import com.jzoom.zoom.admin.adapters.DataAdapters;
import com.jzoom.zoom.dao.annotations.Column;
import com.jzoom.zoom.dao.annotations.ColumnIgnore;
import com.jzoom.zoom.dao.annotations.Table;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Table(name = "sys_deco_table")
public class DecoTableVo {


	public boolean shouldDisplay(String name) {
		return displayColumns == null || displayColumns.contains(name);
	}

    public Map<String, Object> getProps() {
        return props;
    }

    public void setProps(Map<String, Object> props) {
        this.props = props;
    }

	public static class Link{
		private String table;
		private String on;

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        private String type;

		public String getTable() {
			return table;
		}

		public void setTable(String table) {
			this.table = table;
		}

		public String getOn() {
			return on;
		}

		public void setOn(String on) {
			this.on = on;
		}
	}


	@Table(name = "sys_decoration")
	public static class DecoColumn{

		private String table;

		private String column;
		//适配
		private String name;

		private String type;

		private String comment;

		private boolean selected;

		private String orgName;

		private Map<String, Object> prop;

		public String getType() {
			return type;
		}

		public void setType(String type) {
			this.type = type;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getComment() {
			return comment;
		}

		public void setComment(String comment) {
			this.comment = comment;
		}

		public String getColumn() {
			return column;
		}

		public void setColumn(String column) {
			this.column = column;
		}

		public Map<String, Object> getProp() {
			return prop;
		}

		public void setProp(Map<String, Object> prop) {
			this.prop = prop;
		}

		public boolean isSelected() {
			return selected;
		}

		public void setSelected(boolean selected) {
			this.selected = selected;
		}

		public String getTable() {
			return table;
		}

		public void setTable(String table) {
			this.table = table;
		}

		public String getOrgName() {
			return orgName;
		}

		public void setOrgName(String orgName) {
			this.orgName = orgName;
		}
	}

	//target_table,update_at,comment,links,id,ds,fields,columns,filter,tmp_id,filter_columns,display_column


	/**
	 * 原有的id
	 */
	private String id;

	@Column("target_table")
	private String name;

	
	private String comment;

	@ColumnIgnore
	private String[] primaryKeys;

	// join
	private List<Link> links;
	private String ds;
	//模板分组
	@Column("tmp_id")
	private String tmpId;

	@com.jzoom.zoom.dao.annotations.Link(columns = {""})
	private List<DecoColumn> columns;

	@Column(value = "display_columns", adapter = DataAdapters.Set2StringAdapter.class)
	private Set<String> displayColumns;

	@Column("filter_columns")
	private List<DecoColumn> filterColumns;


    // 属性
    private Map<String, Object> props;


	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public List<DecoColumn> getColumns() {
		return columns;
	}

	public void setColumns(List<DecoColumn> columns) {
		this.columns = columns;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String[] getPrimaryKeys() {
		return primaryKeys;
	}

	public void setPrimaryKeys(String[] primaryKeys) {
		this.primaryKeys = primaryKeys;
	}

	public List<Link> getLinks() {
		return links;
	}

	public void setLinks(List<Link> links) {
		this.links = links;
	}

	public String getDs() {
		return ds;
	}

	public void setDs(String ds) {
		this.ds = ds;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getTmpId() {
		return tmpId;
	}


    public boolean isSupportFilter() {
        if (this.filterColumns == null || this.filterColumns.size() == 0) {
            return false;
        }
        return true;
    }

    public void setTmpId(String tmpId) {
		this.tmpId = tmpId;
	}

	public String getPrimaryKey() {
		String[] keys = getPrimaryKeys();
		if(keys==null)return "'id'";
		if(keys.length == 0) {
			return "'id'";
		}
		if(keys.length == 1) {
			return String.format("'%s'", keys[0]);
		}

		StringBuilder sb = new StringBuilder();
		sb.append("[");
		boolean first = true;
		for (String key : keys) {
			if(first)
				first  =false;
			else {
				sb.append(",");
			}
			sb.append("'");
			sb.append(key);
			sb.append("'");
		}

		sb.append("]");
		return sb.toString();
	}

	public Set<String> getDisplayColumns() {
		return displayColumns;
	}

	public void setDisplayColumns(Set<String> displayColumns) {
		this.displayColumns = displayColumns;
	}

	public List<DecoColumn> getFilterColumns() {
		return filterColumns;
	}

	public void setFilterColumns(List<DecoColumn> filterColumns) {
		this.filterColumns = filterColumns;
	}


}
